package mafia.younoodle.ilovemystuff;

import java.util.ArrayList;

import mafia.younoodle.ilovemystuff.data.Category;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public final class CategoryListAdapter extends BaseAdapter {

	private final LayoutInflater inflater;
//	private final int resource;
	private final ArrayList<Category> categories;
	private final Context context;

	public CategoryListAdapter(Context context, ArrayList<Category> categories) {
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.categories = categories;
		this.context = context;
	}
	
//	public CategoryListAdapter(Context context, int resource, ArrayList<Category> categories) {
//		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		this.resource = resource;
//		this.categories = categories;
//	}

	@Override
	public int getCount() {
		return categories.size();
	}

	@Override
	public Object getItem(int position) {
		return categories.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
//		ViewHolder holder;
//		if (convertView == null) {
//			convertView = inflater.inflate(resource, parent, false);
//			
//			holder = new ViewHolder();
//			holder.tvCategoryName = (TextView) convertView.findViewById(R.id.tv_name);
//			convertView.setTag(holder);
//		}
//		else
//			holder = (ViewHolder) convertView.getTag();
//
//		holder.tvCategoryName.setText(categories.get(position).name);
//		return convertView;
		
		Category category = this.categories.get(position);
		return new CategoryListView(this.context, category.name);
	}

	private static final class ViewHolder {
		TextView tvCategoryName;
	}
	
	private final class CategoryListView extends LinearLayout {
		private TextView tvName;
		
		public CategoryListView(Context context, String name) {
			super(context);
			setOrientation(LinearLayout.VERTICAL);
			
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
			params.setMargins(5, 3, 5, 0);
			
			tvName = new TextView(context);
			tvName.setText(name);
			tvName.setTextSize(16f);
			tvName.setTextColor(Color.WHITE);
			addView(tvName, params);
		}
	}
}
